import logging
import os
import smtplib
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime
from email import encoders
from email.header import Header
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

import requests
from flask import request, jsonify

from app.base import base
from app.routes.numberTask import updateTaskStatus, updateTaskSuccessNum

requests.packages.urllib3.disable_warnings()

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)


class Amazon():
    def __init__(self, num, number_s_path):
        self.url = "https://www.amazon.in/ap/signin"
        self.num = num
        self.number_s_path = number_s_path
        self.redirect = None
        self.workflowState = None
        self.appActionToken = None
        self.openid = None
        self.prevRID = None

    def check(self):
        cookies = {"session-id": "262-6899214-0753700", "session-id-time": "2289745062l", "i18n-prefs": "INR",
                   "csm-hit": "tb:6NWTTM14VJ00ZAVVBZ3X+b-36CP76CGQ52N3TB0HZG8|1659025064788&t:1659025064788&adb:adblk_no",
                   "ubid-acbin": "257-4810331-3732018",
                   "session-token": "\"tyoeHgowknphx0Y/CBaiVwnBiwbhUb1PRTvQZQ+07Tq9rmkRD6bErsUDwgq6gu+tA53K6WEAMwOb3pN4Ti3PSFoo+I/Jt5qIEDEMHIeRo1CrE264ogGDHsjge/CwWUZ9bVZtbo32ej/ZPQdm8bYeu6TQhca+UH7Wm9OOwBGoPl7dfoUk79QLYEz69Tt3ik4zMJom8jfgI227qMPuaMaAsw==\""}
        headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:103.0) Gecko/20100101 Firefox/103.0",
                   "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
                   "Accept-Language": "en-US,en;q=0.5", "Accept-Encoding": "gzip, deflate", "DNT": "1",
                   "Content-Type": "application/x-www-form-urlencoded", "Origin": "https://www.amazon.in",
                   "Connection": "close", "X-Forwarded-For": "127.0.0.1",
                   "Referer": "https://www.amazon.in/ap/signin?openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.in%2F%3Fref_%3Dnav_ya_signin&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.assoc_handle=inflex&openid.mode=checkid_setup&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&",
                   "Upgrade-Insecure-Requests": "1", "Sec-Fetch-Dest": "document", "Sec-Fetch-Mode": "navigate",
                   "Sec-Fetch-Site": "same-origin", "Sec-Fetch-User": "?1"}
        data = {"appActionToken": "Aok8C9I71Cr17vp22ONGvDUXR8Yj3D", "appAction": "SIGNIN_PWD_COLLECT",
                "subPageType": "SignInClaimCollect",
                "openid.return_to": "ape:aHR0cHM6Ly93d3cuYW1hem9uLmluLz9yZWZfPW5hdl95YV9zaWduaW4=",
                "prevRID": "ape:MzZDUDc2Q0dRNTJOM1RCMEhaRzg=",
                "workflowState": "eyJ6aXAiOiJERUYiLCJlbmMiOiJBMjU2R0NNIiwiYWxnIjoiQTI1NktXIn0.tCHWdlv4kSSigZCZiGfSCYgnReddxq7c0cUpf0dxYqYzWU-ZHIL0mQ.eP-cXQNtVyBr4q_g.fNRQAD5f18IU0nmqT7IwklJZV-_b60As-_dvyVd4MMjDpiMoGFJ0edbmuL8GJKT_BEE7ClwIpUYOtUtejr7v8qCRy4iD6bg_eBRSnTmZiXzVsx4EuL241zhoriZ7FpXS2seG82sx85C2udl1sPRQyKnO1zIqulOCechL_LzBmIRDv9ngzfij-nYmjWrDpZvAXiKCclR9v0UYh_SqjjOIrStMC53AlWjH-hYdDkXWSeTyHchFi9Ij4ndOgJb9tKNucA4_j7Uy-R0wvB9zlwEfQNa3394guXjjz6IR3TVMjw41bySCYbHLf6j5oj-5xh6UZm2CsW7DE5gqbHmlq5Nv8zLvTRTO9HJvM9Wr36R1eDRN.wZAX4qr9VTROJR9qdWbHfw",
                "email": self.num, "password": '',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7enocx24tAXNcbkSP7YEwf+6FHELZbkF+FaejAwDdCTbbjzt8D6L/G41ftiITJRqhBfi/KMg7UzjjBRw7DPWRbcv7b2zG6ibeiHisSa8Bqc2cilsCss0fBv7mPewzrj5pUBTgifpX/3hbMD/GDRd4SL2Wh6/7E25e70cLOYbz/62NGEFAsoedoJQBjqzvoxtDlXe9AXjOJk3a2Ixcy9EdQjA8+wn73oFQIfzuWIHQ24YnWD5fBKqMA8G0W2tj8YsSbNzOHSDdx92TKUlEhhLO0JDPhN///haxCtJKb56UTFh29u1vddU8WLbJ57yrukGsYCCS/HJv3MjepQiur/9xM/tvUzYd3fhgz5vRwm0QX0ywWsAmF+4UKx75zyLo2AkFi/oaWvMjpXzW2P1+6OQpQUD5Q0WfiHkZpg0r6OrRDaoPZZTsW25gao5dXJ/F+IXK1UqG30/IsBZJP7Gihz7pOqvyH0N2Ck3uWTo9rErjbVK2+f+AwiEWGp9XcF0yXtAUGrXcYDBQo42UbErsfgzrRHyBZv7FdYNuRJXzK5hxFz4LceQQzbZ2CvlpN/zPDdf96k/c1XQ2bect3pm069m2jzFLbtCfVXV2okTw8gRGkg8LZHNBrilrdnMDemErsCcNx8UQBpanXziPC4PoMF8qL2RHWtl3C1NUBTut5DGT9N8EbIP8lcBVnrW1cPGvgL0PoRNsYX7i9tqu7KbSFq/XHaQLUxYFnURxWDHU+0VINZsdkVimRmw4OL2L9hgfgW+unykIIuW8MzaUcFYbg+S3xUEg6JBQu9ygBTSaV4AkKbmka5qrmfD10nz2dSnn5CQ8YQwKD7nQdbGDno1dgrw0jPG2jR7HxvWbg8DRgqoXktexEXDa0+VPoj9Dcb4EZNCM6a+cTMAeFH0ujIDA+ulWgK8NkDthW08LT4pVIQLY/s4hEkTT/OEsXIyy0qG8+6setG76MNq5pYl8oC9/5FBglKT6muvMlT980KtPgi=="}
        res = requests.post(self.url, headers=headers, cookies=cookies, data=data).text
        if "ap_change_login_claim" in res:
            print("数据校验成功", self.num)
            try:
                num_cleaned = self.num.replace("+", "")
                with open(self.number_s_path, 'a+') as success_file:
                    success_file.write("%s\n" % num_cleaned)

            except Exception as e:
                print("数据库操作出错:", str(e))
            return True, res
        elif "There was a problem" in res:
            try:
                print("数据校验失败", self.num)
            except Exception as e:
                print("失败数据库操作出错:", str(e))
            return False, res
        else:
            return False, res


def fun_action(number_s_path, num):
    num = num.strip()
    if num.isnumeric() and "+" not in num:
        num = "+%s" % num
    elif "@" in num:
        pass
    else:
        pass
    while True:
        try:
            A, Error = Amazon(num, number_s_path).check()

            if A:
                break
            else:
                break
        except:
            pass


@base.route('/amazonNumberFilter', methods=['GET'])
def filter_amazon_numbers():
    # 获取当前时间
    current_time = datetime.now()
    # 格式化时间输出
    formatted_time_start = current_time.strftime("%Y-%m-%d %H:%M:%S")
    logger.info(f"亚马逊数据筛查|开始筛选时间: {formatted_time_start}")

    number_g_path = request.args.get('number_g_path')
    number_s_path = request.args.get('number_s_path')

    result = open(number_g_path, 'r').read().splitlines()
    if not result:
        logger.warning(f"亚马逊数据筛查|没有可处理的数据<{number_g_path}>")
        return

    # 定义工作函数
    def worker(a):
        try:
            # 调用你的处理函数 fun_action
            fun_action(number_s_path, a)
        except Exception as e:
            logger.warning(f"亚马逊数据筛查|单线程数据处理异常: 号码{a},异常{e}")

    with ThreadPoolExecutor(max_workers=20) as executor:
        try:
            futures = [executor.submit(worker, a) for a in result]  # 提交所有任务
            for future in futures:
                future.result()  # 等待所有任务完成并捕获可能的异常
        except Exception as e:
            logger.warning(f"亚马逊数据筛查|任务提交失败: 异常{e}")
    current_time = datetime.now()
    # 格式化时间输出
    formatted_time_end = current_time.strftime("%Y-%m-%d %H:%M:%S")
    logger.info(f"亚马逊数据筛查|手机号校验服务数据跑完了<{number_s_path}> 开始时间:{formatted_time_start},结束时间:{formatted_time_end}")

    filename = number_s_path
    if not os.path.exists(filename):
        with open(filename, 'w') as file:
            logger.warning(f"亚马逊数据筛查|没有有效的数据,新建空文件夹<{number_s_path}>")
    # 使用示例
    sender_email = "zdd_king@163.com"
    sender_password = "EBeyjMtvQPxRgMXC"  # 发送方邮箱的授权码或密码
    recipient_email = "zdd_king@163.com"
    email_subject = f"测试邮件服务<开始时间:{formatted_time_start}结束时间:{formatted_time_end}>"
    email_body = f'这是一封来自—测试的邮件.txt'
    # 组装附件
    fetch_numbers_from_file(number_s_path)
    send_email(sender_email, sender_password, recipient_email, email_subject, email_body, number_s_path)
    response = jsonify({"message": "数据处理完毕"})
    response.headers['Content-Type'] = 'application/json; charset=utf-8'  # 明确指定字符集
    return response


def intenalFilterAmazonNumbers(number_g_path, number_s_path, task_id):
    # 获取当前时间
    current_time = datetime.now()
    # 格式化时间输出
    formatted_time_start = current_time.strftime("%Y-%m-%d %H:%M:%S")
    logger.info(f"内部-亚马逊数据筛查|开始筛选时间: {formatted_time_start},号码生成文件路径{number_g_path},号码筛选文件路径{number_s_path}")
    updateTaskStatus(task_id, 3)
    logger.warning(f"内部-亚马逊数据筛查|任务号码号码筛选中<{task_id}>")
    result = open(number_g_path, 'r').read().splitlines()
    if not result:
        logger.warning(f"内部-亚马逊数据筛查|没有可处理的数据<{number_g_path}>")
        return

    # 定义工作函数
    def worker(a):
        try:
            # 调用你的处理函数 fun_action
            fun_action(number_s_path, a)
        except Exception as e:
            logger.warning(f"内部-亚马逊数据筛查|单线程数据处理异常: 号码{a},异常{e}")

    with ThreadPoolExecutor(max_workers=20) as executor:
        try:
            futures = [executor.submit(worker, a) for a in result]  # 提交所有任务
            for future in futures:
                future.result()  # 等待所有任务完成并捕获可能的异常
        except Exception as e:
            logger.warning(f"内部-亚马逊数据筛查|任务提交失败: 异常{e}")
    updateTaskStatus(task_id, 4)
    logger.warning(f"内部-亚马逊数据筛查|任务号码号码筛选完成<{task_id}>")
    # 如果文件存在，检查是否为空
    line_count = 0
    try:
        with open(number_s_path, 'r', encoding='utf-8') as file:
            for line_count, _ in enumerate(file, 1):
                pass
        if line_count > 0:
            logger.info(f"内部-亚马逊数据筛查|任务修改筛选数量<{task_id}> 总条数:{line_count}")
            updateTaskSuccessNum(task_id, line_count)
    except IOError as e:
        logger.error(f"读取文件错误: {e}")
        # 在错误时，你可以选择要执行的逻辑，比如返回或抛出异常


def fetch_numbers_from_file(number_s_path):
    try:
        # 从指定的txt文件中读取号码
        with open(number_s_path, 'r') as file:
            numbers = file.readlines()  # 每行读取一个号码
        # 输出每个号码
        for number in numbers:
            number.strip()  # 去掉换行符和空白
    except Exception as e:
        logger.info(f"亚马逊数据筛查|读取文件失败<{number_s_path}>{e}")


def send_email(sender, password, recipient, subject, body, number_s_path):
    # 创建邮件对象
    msg = MIMEMultipart()
    msg['From'] = Header(sender, 'utf-8')
    msg['To'] = Header(recipient, 'utf-8')
    msg['Subject'] = Header(subject, 'utf-8')
    attachment_path = number_s_path
    # 添加邮件正文
    msg.attach(MIMEText(body, 'plain', 'utf-8'))
    # 处理附件
    try:
        with open(attachment_path, 'rb') as attachment:
            part = MIMEBase('application', 'octet-stream')
            part.set_payload(attachment.read())
            encoders.encode_base64(part)
            part.add_header(
                'Content-Disposition',
                f'attachment; filename={attachment_path.split("/")[-1]}',
            )
            msg.attach(part)
    except Exception as e:
        logger.error(f"亚马逊数据筛查|附件处理失败<{number_s_path}>{e}")
        return
    try:
        # 连接 SMTP 服务器（使用 SSL 连接）
        smtp_server = "smtp.163.com"  # 替换为你的 SMTP 服务器
        server = smtplib.SMTP_SSL(smtp_server, 465)  # SSL 端口通常是 465

        # 登录 SMTP 服务器
        server.login(sender, password)

        # 发送邮件
        server.sendmail(sender, recipient, msg.as_string())
        logger.info(f"亚马逊数据筛查|邮件发送成功<{number_s_path}>")
    except Exception as e:
        logger.error(f"亚马逊数据筛查|邮件发送失败<{number_s_path}>{e}")
    finally:
        # 关闭连接
        server.quit()
